package com.online_exam.controller.admin;

import com.online_exam.base.RestResponse;
import com.online_exam.base.UserType;
import com.online_exam.domain.User;
import com.online_exam.domain.other.KeyValue;
import com.online_exam.service.UserService;
import com.online_exam.util.JwtUtil;
import com.online_exam.viewmodel.student.user.InfoVM;
import com.online_exam.viewmodel.student.user.LoginVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("adminController")
@RequestMapping("/api/admin/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/current" ,method = RequestMethod.POST)
    public RestResponse current(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        User user = userService.getCurrent(UserType.Student,cookies);
        if (user!=null)
            return RestResponse.ok(user);

        return RestResponse.fail(2, "获取当前用户异常！");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public RestResponse update(@RequestBody InfoVM infoVM){
        User user = new User();
        user.setAge(infoVM.getAge());
        user.setBirthDay(infoVM.getBirthDay());
        user.setCreateTime(infoVM.getCreateTime());
        user.setImagePath(infoVM.getImagePath());
        user.setPhone(infoVM.getPhone());
        user.setRealName(infoVM.getRealName());
        user.setUserLevel(infoVM.getUserLevel());
        user.setUserName(infoVM.getUserName());
        user.setSex(infoVM.getSex());
        if (userService.update(user)==1){
            return RestResponse.ok();
        }
        return RestResponse.fail(2, "修改异常！");
    }


    @RequestMapping(value = "/selectByUserName", method = RequestMethod.POST)
    public RestResponse<List<KeyValue>> findUserByUserName(@RequestBody String username) {
        List<KeyValue> name = userService.findByUserName(username);
        System.out.println(name.toString());
        return RestResponse.ok(name);
    }
}
